;(function ($, layer) {
    var init = function (toolbar) {
        initGrid(toolbar);
    };

    var initGrid = function (toolbar) {
        $('#userGrid').datagrid({
            url: './user/findAll',
            title: '用户列表',
            method: 'GET',
            idField: 'id',
            fitColumns: true,
            striped: true,
            rownumbers: true,
            singleSelect: true,
            pagination: true,
            toolbar: toolbar,
            columns: [[
                {field: 'username', title: '用户名', width: '25%', halign: 'center'},
                {field: 'nickname', title: '用户昵称', width: '30%', halign: 'center'},
                {field: 'role', title: '用户角色', width: '25%', halign: 'center', formatter: function (value) {
                    if (value) {
                        return value.roleName;
                    }
                }},
                {field: 'locked', title: '是否锁定', width: '20%', align: 'center', formatter: function (value) {
                    if (value == '1') {
                        return '<span style="color: red">锁定</span>';
                    } else {
                        return '<span style="color: green">可用</span>';
                    }
                }}
            ]],
            loadFilter: function (data) {
                var newData = {};
                if (data) {
                    newData.total = data.total;
                    newData.rows = data.list;
                    return newData;
                }
            }
        });
    };

    var initDialog = function (title, callback) {
        $('#userDialog').dialog({
            title: title,
            modal: true,
            border: 'thin',
            onClose: function () {
                callback();
            }
        });
    };

    var initAddForm = function (url) {
        var setting = {
            id: 'userForm',
            url: url,
            method: 'post',
            appendTo: '#userDialog',
            items: [
                {
                    type: 'textbox',
                    id: 'username',
                    name: 'username',
                    option: {
                        label: '用户名:',
                        required: true,
                        validType: ['length[5,20]']
                    }
                }, {
                    type: 'passwordbox',
                    id: 'password',
                    name: 'password',
                    option: {
                        label: '密码',
                        required: true,
                        validType: ['length[5,20]']
                    }
                }, {
                    type: 'textbox',
                    id: 'nickname',
                    name: 'nickname',
                    option: {
                        label: '用户昵称:',
                        required: true,
                        validType: ['length[0,20]']
                    }
                }, {
                    type: 'combobox',
                    id: 'role',
                    name: 'role',
                    option: {
                        label: '用户角色',
                        url: './role/findAll',
                        method: 'GET',
                        limitToList: true,
                        valueField: 'id',
                        textField: 'roleName',
                        loadFilter: function (data) {
                            if (data) {
                                return data.list;
                            }
                        }
                    }
                }, {
                    type: 'combobox',
                    id: 'locked',
                    name: 'locked',
                    option: {
                        label: '是否锁定:',
                        required: true,
                        data: [{value: 1, text: '锁定'}, {value: 0, text: '可用'}],
                        value: 0
                    }
                }
            ],
            btns: ['submit["$.user.reloadData"]', 'reset']
        };

        return $.zUtil.form(setting);
    };

    var add = function () {
        var url = './user/add';
        var form = initAddForm(url);
        initDialog('新建用户', function () {
            form.destroy();
        });
    };

    var edit = function () {
        var selRow = $('#userGrid').treegrid('getSelected');
        if (!selRow) {
            layer.msg('请选择一条记录！');
            return;
        }
        var url = './user/edit/' + selRow.id;
        var form = initEditForm(selRow, url);
        initDialog('编辑用户', function () {
            form.destroy();
        });
    };

    var initEditForm = function (data, url) {
        var setting = {
            id: 'userForm',
            url: url,
            method: 'post',
            appendTo: '#userDialog',
            items: [
                {
                    type: 'textbox',
                    id: 'username',
                    name: 'username',
                    option: {
                        label: '用户名:',
                        required: true,
                        validType: ['length[5,20]'],
                        value: data.username
                    }
                }, {
                    type: 'textbox',
                    id: 'nickname',
                    name: 'nickname',
                    option: {
                        label: '用户昵称:',
                        required: true,
                        validType: ['length[0,20]'],
                        value: data.nickname
                    }
                }, {
                    type: 'combobox',
                    id: 'role',
                    name: 'role',
                    option: {
                        label: '用户角色',
                        url: './role/findAll',
                        method: 'GET',
                        limitToList: true,
                        valueField: 'id',
                        textField: 'roleName',
                        loadFilter: function (data) {
                            if (data) {
                                console.log(data.list);
                                return data.list;
                            }
                        },
                        value: data.role ? data.role.id : null
                    }
                }, {
                    type: 'combobox',
                    id: 'locked',
                    name: 'locked',
                    option: {
                        label: '是否锁定:',
                        required: true,
                        data: [{value: 1, text: '锁定'}, {value: 0, text: '可用'}],
                        value: data.locked ? 1 : 0
                    }
                }
            ],
            btns: ['submit["$.user.reloadData"]', 'reset']
        };

        return $.zUtil.form(setting);
    };

    var destroy = function () {
        var selRow = $('#userGrid').treegrid('getSelected');
        if (!selRow) {
            layer.msg('请选择一条记录！');
            return;
        }
        layer.confirm('您确定删除此用户!', {icon: 3, title: '提示'}, function (e) {
            $.ajax({
                url: './user/destroy/' + selRow.id,
                type: 'DELETE',
                dataType: 'JSON',
                success: function (data) {
                    if (data.success) {
                        layer.msg('保存成功', {time: 1000});
                        $('#userGrid').datagrid('reload');
                        layer.close(e);
                    } else {
                        layer.msg(data.message, {time: 1000});
                    }
                }
            })
        })
    };

    //重置密码
    var resetPassword = function () {
        var selRow = $('#userGrid').treegrid('getSelected');
        if (!selRow) {
            layer.msg('请选择一条记录！');
            return;
        }
        var setting = {
            url: 'user/resetPassword/' + selRow.id,
            method: 'post',
            appendTo: '#resetPasswordDialog',
            items: [
                {
                    type: 'passwordbox',
                    id: 'password',
                    name: 'password',
                    option: {
                        label: '密码',
                        required: true,
                        validType: ['length[5,20]']
                    }
                }, {
                    type: 'passwordbox',
                    id: 'confirmPassword',
                    option: {
                        label: '确认密码',
                        required: true,
                        validType: ['length[5,20]', 'equals["#password"]']
                    }
                }
            ],
            btns: ['submit', 'reset']
        };
        var form = $.zUtil.form(setting, function () {
            layer.msg('保存成功', {time: 1000}, function () {
                $('#resetPasswordDialog').dialog('close');
            });
        });
        $('#resetPasswordDialog').dialog({
            title: '重置密码',
            modal: true,
            onClose: function () {
                form.destroy();
            }
        });
    };

    $.extend({
        user: {
            init: init,
            add: add,
            edit: edit,
            destroy: destroy,
            resetPassword: resetPassword,
            reloadData: function () {
                layer.msg('保存成功！', {time: 1000}, function () {
                    $('#userDialog').dialog('close');
                    $('#userGrid').datagrid('reload');
                });
            }
        }
    });
})(jQuery, layer);